package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.XcxSubscribeProjectUnitBo;
import org.dromara.system.domain.vo.XcxSubscribeProjectUnitVo;
import org.dromara.system.domain.XcxSubscribeProjectUnit;
import org.dromara.system.mapper.XcxSubscribeProjectUnitMapper;
import org.dromara.system.service.IXcxSubscribeProjectUnitService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订阅项目单位Service业务层处理
 *
 * @author Lion Li
 * @date 2024-07-01
 */
@RequiredArgsConstructor
@Service
public class XcxSubscribeProjectUnitServiceImpl implements IXcxSubscribeProjectUnitService {

    private final XcxSubscribeProjectUnitMapper baseMapper;

    /**
     * 查询订阅项目单位
     */
    @Override
    public XcxSubscribeProjectUnitVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订阅项目单位列表
     */
    @Override
    public TableDataInfo<XcxSubscribeProjectUnitVo> queryPageList(XcxSubscribeProjectUnitBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<XcxSubscribeProjectUnit> lqw = buildQueryWrapper(bo);
        Page<XcxSubscribeProjectUnitVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订阅项目单位列表
     */
    @Override
    public List<XcxSubscribeProjectUnitVo> queryList(XcxSubscribeProjectUnitBo bo) {
        LambdaQueryWrapper<XcxSubscribeProjectUnit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XcxSubscribeProjectUnit> buildQueryWrapper(XcxSubscribeProjectUnitBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<XcxSubscribeProjectUnit> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, XcxSubscribeProjectUnit::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectUnit()), XcxSubscribeProjectUnit::getProjectUnit, bo.getProjectUnit());
        return lqw;
    }

    /**
     * 新增订阅项目单位
     */
    @Override
    public Boolean insertByBo(XcxSubscribeProjectUnitBo bo) {
        XcxSubscribeProjectUnit add = MapstructUtils.convert(bo, XcxSubscribeProjectUnit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订阅项目单位
     */
    @Override
    public Boolean updateByBo(XcxSubscribeProjectUnitBo bo) {
        XcxSubscribeProjectUnit update = MapstructUtils.convert(bo, XcxSubscribeProjectUnit.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(XcxSubscribeProjectUnit entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订阅项目单位
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
